package com.zk.code.net;

import android.hardware.Camera;

import java.io.*;

public class FileUtil {

    public static byte[] File2byte(String filePath)
    {
        byte[] buffer = null;
        try
        {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return buffer;
    }

    public static byte[] imageToBytes(String path) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(path);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        return data;
    }


    public static File byte2File(byte[] buf, String filePath, String fileName)
    {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try
        {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory())
            {
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (bos != null)
            {
                try
                {
                    bos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (fos != null)
            {
                try
                {
                    fos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }


    public static boolean isCameraCanUse(int channelno) {
        boolean canUse = false;
        Camera mCamera = null;
        try {
            mCamera = Camera.open(channelno);
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            canUse = false;
        }
        if (mCamera != null) {
            mCamera.release();
            canUse = true;
        }
        return canUse;
    }




    public static boolean isAllCameraCanUse() {
        boolean canUse0 = false;
        boolean canUse1 = false;
        boolean canUse = false;
        Camera mCamera0 = null;
        Camera mCamera1 = null;
        try {
            mCamera0 = Camera.open(0);
            Camera.Parameters mParameters = mCamera0.getParameters();
            mCamera0.setParameters(mParameters);
        } catch (Exception e) {
            canUse0 = false;
        }
        if (mCamera0 != null) {
            mCamera0.release();
            canUse0 = true;
        }


        try {
            mCamera1 = Camera.open(1);
            Camera.Parameters mParameters = mCamera1.getParameters();
            mCamera1.setParameters(mParameters);
        } catch (Exception e) {
            canUse1 = false;
        }
        if (mCamera1 != null) {
            mCamera1.release();
            canUse1 = true;
        }

        if(!canUse1&&!canUse0){
            canUse = false;
        }else{
            canUse = true;
        }
        return canUse;
    }
}
